﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BAL;
using System.Configuration;

namespace ProjectQuaDep.Control
{
    public partial class Upload : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                //BinDataToCboYear();
            }
        }

        protected void BinDataToCboYear()
        {
            int currentYear = DateTime.Now.Year;
            ListItem objItem;
            drYear.Items.Add(new ListItem("Năm", "00"));
            for (int i = currentYear; i >= 1980; i--)
            {
                objItem = new ListItem();
                objItem.Text = i.ToString();
                objItem.Value = i.ToString();
                drYear.Items.Add(objItem);
                drYear.SelectedValue = "00";
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            int number = 0;  
            if (!string.IsNullOrEmpty(hidUploadImg.Value))
            {
                string[] arrImg = null;
                if (hidUploadImg.Value.Contains(','))
                {
                    arrImg = hidUploadImg.Value.Split(',');
                    for (int i = 0; i < arrImg.Length - 1; i++)
                    {
                        string acc = string.Empty;
                        string Name = txtTenBe.Text;
                        string Email = txtEmail.Text;
                        string Address = txtAddress.Text;
                        string Phone = txtPhone.Text;
                        string Other = txtParent.Text;
                        string linkFB = txtFB.Text;
                        string Img = arrImg[i];
                        string Title = string.Empty;
                        string ShortContent = txtChiaSe.Text;
                        string LongContent = string.Empty;
                        int RoundAward = Utils.GetCurrentRound();
                        string Birthday = drYear.SelectedValue + drMonth.SelectedValue + drDay.SelectedValue;
                        string Search = Utils.RemoveUnicode(Name);
                        number = BussinessHelpers.InsertImage(acc, Name, Search, Email, Address, Phone, Other, Img, Title, ShortContent, LongContent, RoundAward, Birthday, linkFB);
                    }
                }
                else
                {
                    string acc = string.Empty;
                    string Name = txtTenBe.Text;
                    string Email = txtEmail.Text;
                    string Address = txtAddress.Text;
                    string Phone = txtPhone.Text;
                    string Other = txtParent.Text;
                    string linkFB = txtFB.Text;
                    string Img = hidUploadImg.Value;
                    string Title = string.Empty;
                    string ShortContent = txtChiaSe.Text;
                    string LongContent = string.Empty;
                    int RoundAward = Utils.GetCurrentRound();
                    string Birthday = drYear.SelectedValue + drMonth.SelectedValue + drDay.SelectedValue;
                    string Search = Utils.RemoveUnicode(Name);
                    number = BussinessHelpers.InsertImage(acc, Name, Search, Email, Address, Phone, Other, Img, Title, ShortContent, LongContent, RoundAward, Birthday, linkFB);
                   
                }
                if (number > 0)
                {
                    Response.Write("<script>alert('Ban tổ chức đã nhận được ảnh của bạn và đang xử lý. Chúng tôi sẽ gửi email xác nhận ngay sau khi xử lý xong, muộn nhất sau 18 tiếng!');window.location='/huong-dan.htm';</script>");
                    try
                    {
                        string pGmailEmail = BO.Const.EmailSend;//"dungngtran87";
                        string pGmailPassword = BO.Const.PassEmail;//"longthan";
                        string pBody = "Tên : "+txtTenBe.Text+" Email: "+txtEmail.Text+" đã đăng ký dự thi thành công</a>";
                        string pTo = ConfigurationManager.AppSettings["ToEmail"].ToString();//"dungnguyentran@channelvn.net";//"nguyenhaitien@vneconomy.vn";
                        string pSubject = BO.Const.pSubject;//"Immukid cuộc thi ảnh";
                        System.Web.Mail.MailFormat pFormat = System.Web.Mail.MailFormat.Html;
                        SendMail(pGmailEmail, pGmailPassword, pTo, pSubject, pBody, pFormat, "");
                    }
                    catch (Exception ex)
                    {
                    }
                }
            }
        }

        protected void SendMail(string pGmailEmail, string pGmailPassword, string pTo, string pSubject, string pBody, System.Web.Mail.MailFormat pFormat, string pAttachmentPath)
        {
            System.Web.Mail.MailMessage myMail = new System.Web.Mail.MailMessage();
            myMail.BodyEncoding = System.Text.Encoding.UTF8;
            myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserver", "smtp.gmail.com");
            myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", "465");
            myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusing", "2");
            myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
            //Use 0 for anonymous
            myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", pGmailEmail);
            myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", pGmailPassword);
            myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", "true");
            myMail.From = pGmailEmail + "@gmail.com";
            myMail.To = pTo;
            myMail.Subject = pSubject;
            myMail.BodyFormat = pFormat;
            myMail.Body = pBody;
            if (pAttachmentPath.Trim() != "")
            {
                System.Web.Mail.MailAttachment MyAttachment = new System.Web.Mail.MailAttachment(pAttachmentPath);
                myMail.Attachments.Add(MyAttachment);
            }
            myMail.Priority = System.Web.Mail.MailPriority.High;
            System.Web.Mail.SmtpMail.SmtpServer = "smtp.gmail.com:465";
            System.Web.Mail.SmtpMail.Send(myMail);
        }
    }
}